package jst.householdmanager.database;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import jst.householdmanager.GlobalFunc;
import jst.householdmanager.database.table.PurchaseListPos;

public class PurchaseListPosDataSource {

	public static final String TABLE_NAME = "PurchaseListPos";
	public static final String COLUMN_ITEM_ID = "purLstP_item_ID";
	public static final String COLUMN_FINISH_DATE = "purLstP_FinDate";
	public static final String COLUMN_AMOUNT = "purLstP_amount";
	public static final String COLUMN_COMMENT = "purLst_comment";

	public static final String[] ALL_COLUMNS = { COLUMN_ITEM_ID, COLUMN_FINISH_DATE, COLUMN_AMOUNT, COLUMN_COMMENT };

	public static final int[] COLUMN_DATATYPE = { 0, 1, 0, 0, 1 };

	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_ITEM_ID + " INTEGER NOT NULL, " + COLUMN_FINISH_DATE
			+ " DATE, " + COLUMN_AMOUNT + " INTEGER NOT NULL, " + COLUMN_COMMENT + " TEXT, PRIMARY KEY ( " + COLUMN_ITEM_ID + ", "
			+ COLUMN_FINISH_DATE + ") );";

	public static void save(PurchaseListPos dataRecord) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_AMOUNT, dataRecord.getAmount());
		values.put(COLUMN_COMMENT, dataRecord.getComment());

		// check if purchaselistpositon exists in database
		PurchaseListPos dbPurchaseListPos = read(dataRecord.getItem_Id(), dataRecord.getFinishDate());
		if (dbPurchaseListPos != null) {
			// update			
			SQLiteDataSource.getDatabase().update(TABLE_NAME, values, COLUMN_ITEM_ID + " = ? and " + COLUMN_FINISH_DATE + " = ?",
					new String[] { Integer.toString(dataRecord.getItem_Id()), GlobalFunc.DateToString(dataRecord.getFinishDate()) });
		} else {
			// insert
			values.put(COLUMN_ITEM_ID, dataRecord.getItem_Id());
			values.put(COLUMN_FINISH_DATE, GlobalFunc.DateToString(dataRecord.getFinishDate()));
			SQLiteDataSource.getDatabase().insert(TABLE_NAME, null, values);
		}
	}

	public static void delete(String selection, String[] selectionArgs) {
		SQLiteDataSource.getDatabase().delete(TABLE_NAME, selection, selectionArgs);
	}

	public static PurchaseListPos read(String selection, String[] selectionArgs) {
		PurchaseListPos dataRecord = null;
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, null, null, null);
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			dataRecord = cursorTo(cursor);
		}
		cursor.close();

		return dataRecord;
	}

	public static List<PurchaseListPos> readAll(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
		List<PurchaseListPos> dataRecords = new ArrayList<PurchaseListPos>();
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, groupBy, having, orderBy);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			dataRecords.add(cursorTo(cursor));
			cursor.moveToNext();
		}
		cursor.close();

		return dataRecords;
	}

	public static void delete(PurchaseListPos dataRecord) {
		delete(COLUMN_ITEM_ID + " = ? and " + COLUMN_FINISH_DATE + " = ?",
				new String[] { Integer.toString(dataRecord.getItem_Id()), GlobalFunc.DateToString(dataRecord.getFinishDate()) });
	}

	public static PurchaseListPos read(int item_Id, Date finishDate) {
		return read(COLUMN_ITEM_ID + " = ? and " + COLUMN_FINISH_DATE + " = ?",
				new String[] { Integer.toString(item_Id), GlobalFunc.DateToString(finishDate) });
	}

	public static List<PurchaseListPos> readAll(String selection, String[] selectionArgs) {
		return readAll(selection, selectionArgs, null, null, null);
	}

	public static List<PurchaseListPos> readAll() {
		return readAll(null, null, null, null, null);
	}

	private static PurchaseListPos cursorTo(Cursor cursor) {
		return new PurchaseListPos(cursor.getInt(0), GlobalFunc.StringToDate(cursor.getString(1)), cursor.getInt(2), cursor.getString(3));
	}

}
